package com.xingmang.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/4/17 12:41
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {
    private static final List<String> IGNORE_URL = Arrays.asList(
            "/doc.*",
            "/api/*",
            "/favicon.ico",
            "/swagger-ui.*",
            "/swagger-resources",
            "/webjars/**",
            "/v2/api-docs/**",
            "/login/in",
            "/admin/login/in",
            "/admin/login/register",
            "/template/template-data/data-list",
            "/version/hidden",
            "/test/**",
            "/excel/**",
            "/provider/test",
            "/third/**"
    );
    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor(handle -> {
            SaRouter.match("/**").notMatch(IGNORE_URL).check(StpUtil::checkLogin);
        }));
    }
}
